from django.db import models
from django.utils import timezone
from company.models import UserProfile


# 创业流程模板表
class ProcessTemplate(models.Model):
    CATEGORY_CHOICES = [
        ('registration', '企业注册'),
        ('finance', '财务报备'),
        ('funding', '融资申报'),
        ('license', '许可证办理'),
    ]

    name = models.CharField(max_length=200, verbose_name='流程名称', db_comment='流程名称')
    category = models.CharField(max_length=50, choices=CATEGORY_CHOICES, verbose_name='流程类别', db_comment='流程类别:registration企业注册,finance财务报备,funding融资申报,license许可证办理')
    description = models.TextField(verbose_name='流程说明', db_comment='流程说明')
    steps = models.JSONField(default=list, verbose_name='流程步骤', db_comment='流程步骤JSON')
    required_documents = models.JSONField(default=list, verbose_name='所需材料', db_comment='所需材料JSON')
    estimated_days = models.IntegerField(default=0, verbose_name='预计天数', db_comment='预计天数')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_process_templates'
        verbose_name = '创业流程模板'
        verbose_name_plural = '创业流程模板'
        db_table_comment = '创业流程模板表'

    def __str__(self):
        return self.name


# 用户流程进度表
class UserProcess(models.Model):
    STATUS_CHOICES = [
        ('not_started', '未开始'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('failed', '已失败'),
    ]

    user_profile = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name='user_processes',
        verbose_name='用户',
        db_comment='用户ID'
    )
    template = models.ForeignKey(
        ProcessTemplate,
        on_delete=models.CASCADE,
        related_name='user_processes',
        verbose_name='流程模板',
        db_comment='流程模板ID'
    )
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='not_started', verbose_name='状态', db_comment='状态:not_started未开始,in_progress进行中,completed已完成,failed已失败')
    current_step = models.IntegerField(default=0, verbose_name='当前步骤', db_comment='当前步骤')
    progress_data = models.JSONField(default=dict, verbose_name='进度数据', db_comment='进度数据JSON')
    started_at = models.DateTimeField(null=True, blank=True, verbose_name='开始时间', db_comment='开始时间')
    completed_at = models.DateTimeField(null=True, blank=True, verbose_name='完成时间', db_comment='完成时间')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_user_processes'
        verbose_name = '用户流程进度'
        verbose_name_plural = '用户流程进度'
        ordering = ['-created_at']
        db_table_comment = '用户流程进度表'

    def __str__(self):
        return f"{self.user_profile} - {self.template.name}"


# 材料文件表
class ProcessDocument(models.Model):
    STATUS_CHOICES = [
        ('pending', '待上传'),
        ('uploaded', '已上传'),
        ('verified', '已审核'),
        ('rejected', '已拒绝'),
    ]

    user_process = models.ForeignKey(
        UserProcess,
        on_delete=models.CASCADE,
        related_name='documents',
        verbose_name='所属流程',
        db_comment='所属流程ID'
    )
    document_name = models.CharField(max_length=200, verbose_name='文件名称', db_comment='文件名称')
    document_type = models.CharField(max_length=100, verbose_name='文件类型', db_comment='文件类型')
    file_path = models.FileField(upload_to='process_documents/', blank=True, null=True, verbose_name='文件路径', db_comment='文件路径')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态', db_comment='状态:pending待上传,uploaded已上传,verified已审核,rejected已拒绝')
    ai_feedback = models.TextField(blank=True, null=True, verbose_name='AI审核意见', db_comment='AI审核意见')
    uploaded_at = models.DateTimeField(null=True, blank=True, verbose_name='上传时间', db_comment='上传时间')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_process_documents'
        verbose_name = '流程文件'
        verbose_name_plural = '流程文件'
        ordering = ['-created_at']
        db_table_comment = '流程文件表'

    def __str__(self):
        return f"{self.user_process.template.name} - {self.document_name}"
